//
//  loginViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/11.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "loginViewController.h"
#import "MyTabBarController.h"
#import "forgetViewController.h"

@interface loginViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *phoneTextField;
@property (nonatomic,strong) UITextField *pwdTextField;

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ----------------------- init ---------------------

- (void)initView{
    
    self.title = @"账号登录";

    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.delegate = self;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTextField.returnKeyType = UIReturnKeyNext;
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTextField.font = get4sFont(12);
    [self.view addSubview:_phoneTextField];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(31));
        make.left.mas_equalTo(self.view).with.offset(screenScale(25));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-31));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *phoneThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(62), screenScale(335), screenScale(0.5))];
    phoneThread.backgroundColor = getThreadColor();
    [self.view addSubview:phoneThread];
    
    _pwdTextField = [[UITextField alloc]init];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.delegate = self;
    _pwdTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.returnKeyType = UIReturnKeyGo;
    _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _pwdTextField.font = get4sFont(12);
    [self.view addSubview:_pwdTextField];
    [_pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(80));
        make.left.mas_equalTo(self.view).with.offset(screenScale(25));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-31));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *pwdThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(112), screenScale(335), screenScale(0.5))];
    pwdThread.backgroundColor = getThreadColor();
    [self.view addSubview:pwdThread];
    
    UIButton *loginBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(20), screenScale(137.5), screenScale(335), screenScale(44))
                                             title:@"登录"
                                        titleColor:getColor(@"ffffff")
                                              font:get4sFont(12)
                                   backgroundColor:getColor(@"3a95ff")
                                             image:nil];
    loginBtn.layer.cornerRadius = screenScale(3);
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    UIButton *forgetBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(296), screenScale(197), screenScale(60), screenScale(15))
                                             title:@"忘记密码"
                                        titleColor:getColor(@"3a95ff")
                                              font:get4sFont(12)
                                   backgroundColor:getColor(@"ffffff")
                                             image:nil];
    forgetBtn.layer.cornerRadius = screenScale(3);
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
}

#pragma mark ----------------------- click ---------------------

- (void)loginBtnClick{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _phoneTextField.text forKey:@"phone"];
    [productParams setValue: _pwdTextField.text forKey:@"pass"];
    
//    [productParams setValue: @"13610886592" forKey:@"phone"];
//    [productParams setValue: @"123456" forKey:@"pass"];
//    
//    [productParams setValue: @"18624045751" forKey:@"phone"];
//    [productParams setValue: @"a123456" forKey:@"pass"];
    
    [[MNetworkHandler sharedInstance]conURL:login_login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       [PublicFunction saveConfig:returnData[@"data"][0][@"token"] forKey:@"token"];
                                       [PublicFunction saveConfig:returnData[@"data"][0][@"member_id"] forKey:@"member_id"];
                                       
                                       NSLog(@"\ntoken===%@\nmember_id===%@",[PublicFunction getConfigWithKey:@"token"],[PublicFunction getConfigWithKey:@"member_id"]);
                                       
                                       MyTabBarController *barController = [[MyTabBarController alloc] init];
                                       barController.view.backgroundColor = getColor(@"ffffff");
                                       [self presentViewController:barController animated:YES completion:nil];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];

}

- (void)forgetBtnClick{

    forgetViewController *vc = [[forgetViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == _phoneTextField) {
        
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength > 11) {
            return NO;
        }
    }
    
    if (textField == _pwdTextField) {
        
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength > 16) {
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == _phoneTextField) {
        
        [_pwdTextField becomeFirstResponder];
        
    }else if (textField == _pwdTextField){
        
        [self loginBtnClick];
    }
    return YES;
}

@end
